package com.css.apps.base.org.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.log.model.LogConstant;
import com.css.apps.base.log.model.LogPart;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.org.model.SUserOrg;
import com.css.apps.base.user.model.SUser;
import com.css.apps.base.user.service.UserHelper;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Json;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class AddOrgUserOrg extends AdminAction {

	private static Log log = LogFactory.getLog(AddOrgUserOrg.class);
	private String orgId;
	private String ids;
	private String orgids;
	private SOrg item;

	public AddOrgUserOrg() {
		this.item = new SOrg();
	}

	@SuppressWarnings("unchecked")
	public String adminGo() {
		TransactionCache tx = null;
		try {
			if (StringHelper.isEmpty(ids) || StringHelper.isEmpty(orgId) || StringHelper.isEmpty(orgids)) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.idsEmpty"));
				return Action.ERROR;
			}
			List<String> userIdList = StringHelper.strToList(ids);
			List<String> orgIdList = StringHelper.strToList(orgids);
			List<SUser> userList = new ArrayList<SUser>();
			List<SUserOrg> userorgList = new ArrayList<SUserOrg>();
			for (int i = 0; i < userIdList.size(); i++) {
				String userId = userIdList.get(i);
				if (StringHelper.isNotEmpty(userId)) {
					List<String> sorguserId = new QueryCache(
							"select a.uuid from SUserOrg a where a.userId=:userId and a.orgId =:orgId")
									.setParameter("userId", userId).setParameter("orgId", orgIdList.get(i)).list();
					if (sorguserId != null && sorguserId.size() != 0) {
						SUserOrg userorgitem = QueryCache.get(SUserOrg.class, sorguserId.get(0));
						userorgitem.setOrgId(orgId);
						userorgList.add(userorgitem);
					}
					SUser user = QueryCache.get(SUser.class, userId);
					user.setOrgId(orgId);
					userList.add(user);
				}
			}

			SOrg tmp = QueryCache.get(SOrg.class, orgId);
			tx = new TransactionCache();
			StringBuffer sd = new StringBuffer("delete from SUserOrg a where a.orgId=:orgId");
			Map<String, Object> params = new HashMap<>();
			params.put("orgId", orgId);
			tx.executeUpdate(sd.toString(), params);
			tmp.getUserList().removeAll();
			for (String userId : userIdList) {
				SUserOrg userorg = new SUserOrg();
				userorg.setOrgId(orgId);
				userorg.setUserId(userId);
				tx.save(userorg);
				tmp.getUserList().add(userId);
			}

			// tx.update(userList);
			// tx.update(userorgList);
			tx.commit();

			UserHelper helper = new UserHelper();
			for (SUser user : userList) {
				LogPart lp = new LogPart();
				lp.setOpObjType(SUser.class.getName());
				lp.setOpObjId(user.getUuid());
				lp.setRelObjType(SOrg.class.getName());
				lp.setRelObjId(orgId);
				lp.setOpType(LogConstant.LOG_TYPE_ADD);
				lp.setLogLevel(LogConstant.LOG_LEVEL_COMMON);
				lp.setLogData(Json.object2json(user));
				lp.setResult(LogConstant.RESULT_SUCCESS);
				lp.save();
				helper.clearUserMenuCache(user.getUuid());
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;
		} catch (Exception ex) {
			if (tx != null) {
				tx.rollback();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			log.error(ex.getMessage(), ex);
			return Action.ERROR;
		}
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public String getOrgids() {
		return orgids;
	}

	public void setOrgids(String orgids) {
		this.orgids = orgids;
	}

	public SOrg getItem() {
		return item;
	}

	public void setItem(SOrg item) {
		this.item = item;
	}

}